package com.digitalchina.lxnsy.activity.old;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.tabs.TabLayout;
import androidx.legacy.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessCollect;
import com.digitalchina.lxnsy.api.BusinessMerchant;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.MerchantInfoVO;
import com.digitalchina.lxnsy.entity.PagerItemVO;
import com.digitalchina.lxnsy.fragment.GoodsAllFragment;
import com.digitalchina.lxnsy.fragment.GoodsQualityFragment;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/1/10 14:05
 * 修改备注：
 * ================================================
 */
public class MerchantActivity extends BaseActivity {


    @Bind(R.id.merchant_sdv_top)
    SimpleDraweeView merchantSdvTop;
    @Bind(R.id.merchant_tv_collect)
    TextView merchantTvCollect;
    @Bind(R.id.merchant_sdv_logo)
    SimpleDraweeView merchantSdvLogo;
    @Bind(R.id.merchant_iv_grade1)
    ImageView merchantIvGrade1;
    @Bind(R.id.merchant_iv_grade2)
    ImageView merchantIvGrade2;
    @Bind(R.id.merchant_iv_grade3)
    ImageView merchantIvGrade3;
    @Bind(R.id.merchant_iv_grade4)
    ImageView merchantIvGrade4;
    @Bind(R.id.merchant_iv_grade5)
    ImageView merchantIvGrade5;
    @Bind(R.id.merchant_layout_grade)
    LinearLayout merchantLayoutGrade;
    @Bind(R.id.merchant_tv_merchant_name)
    TextView merchantTvMerchantName;
    @Bind(R.id.merchant_relayout)
    RelativeLayout merchantRelayout;
    @Bind(R.id.merchant_tabs)
    TabLayout merchantTabs;
    @Bind(R.id.merchant_collapse_toolbar)
    CollapsingToolbarLayout merchantCollapseToolbar;
    @Bind(R.id.merchant_viewpager)
    ViewPager merchantViewpager;
    @Bind(R.id.merchant_iv_collect_status)
    ImageView merchantIvCollectStatus; // 收藏状态
    @Bind(R.id.merchant_ll_collect_layout)
    LinearLayout merchantLlCollectLayout; // 收藏按钮
    /*每个 tab 的 item*/
    private List<PagerItemVO> mList = new ArrayList<>();
    private String merchantId;
    private Bundle bundle;

    private MerchantInfoVO merchantInfoVO;
    private String commodityCount; //  商品总数
    private String collectType = ""; // 收藏类型
    private boolean isMerchantsCollected = false;//默认商户没有收藏

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_merchant);
        setTitle("商户首页");
    }

    private String url = "http://ww4.sinaimg.cn/large/006uZZy8jw1faic1xjab4j30ci08cjrv.jpg";

    @Override
    public void initMember() {

        if (getIntent().hasExtra("merchantId")) {
            merchantId = (String) getIntent().getSerializableExtra("merchantId");
            BusinessMerchant.showMerchantHome(this, merchantId, mHandler);
            bundle = new Bundle();
            bundle.putString("merchantId", merchantId);
        }

        FrescoUtil.showImageForURL(url, merchantSdvTop);

        setupViewPager();

        setupCollapsingToolbar();

    }


    private void setupViewPager() {
        setupViewPager(merchantViewpager);

        merchantTabs.setupWithViewPager(merchantViewpager);
    }

    private void setupViewPager(ViewPager viewPager) {

        ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
        GoodsQualityFragment goodsQualityFragment = new GoodsQualityFragment();
        GoodsAllFragment goodsAllFragment = new GoodsAllFragment();
        if (bundle != null) {
            goodsQualityFragment.setArguments(bundle);
            goodsAllFragment.setArguments(bundle);
        }
        adapter.addFrag(goodsQualityFragment, "精品商品");
        adapter.addFrag(goodsAllFragment, "全部商品");

        viewPager.setAdapter(adapter);
    }

    private void setupCollapsingToolbar() {

        merchantCollapseToolbar.setTitleEnabled(false);
    }


    @Override
    public void addListener() {
        merchantSdvLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GotoUtil.gotoActivity(MerchantActivity.this, MerchantsIntroduceActivity.class, "merchantId", merchantId);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_SHOW_MERCHANT_HOME_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_SHOW_MERCHANT_HOME_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_IS_STORE_SUCCESS://是否已经收藏
                        String isCollect = FastJsonUtil.getString(msg.obj.toString(), "data");
                        setCollectImg(isCollect);
                        break;
                    case MSG.MSG_IS_STORE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_CANCEL_STORE_SUCCESS:// 取消收藏
                        setCollectImg("false");
                        showToast("取消收藏");
                        break;
                    case MSG.MSG_CANCEL_STORE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_STORE_SUCCESS: // 收藏
                        setCollectImg("true");
                        showToast("收藏成功");
                        break;
                    case MSG.MSG_STORE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    //根据是否已收藏状态显示不同图片
    private void setCollectImg(String collected) {
        if (collected.equals("true")) {
            isMerchantsCollected = true;
            merchantTvCollect.setText("取消收藏");
            merchantIvCollectStatus.setImageResource(R.drawable.ic_merchant_collect_true);
        } else {
            isMerchantsCollected = false;
            merchantTvCollect.setText("收藏");
            merchantIvCollectStatus.setImageResource(R.drawable.ic_merchant_collect_false);
        }
    }

    @Override
    public void callBack(Object object) {
        merchantInfoVO = FastJsonUtil.getBean(object.toString(), "body", "merchantInfo", MerchantInfoVO.class);
        commodityCount = FastJsonUtil.getString(object.toString(), "commodityCount");
        setContent();
    }

    /**
     * 数据填充
     */
    private void setContent() {
        if (null == merchantInfoVO)
            return;
        isCollect();
        FrescoUtil.showImageSmall(merchantInfoVO.getHeadPortrait() != null ? merchantInfoVO.getHeadPortrait() : "", merchantSdvLogo);
        merchantTvMerchantName.setText(merchantInfoVO.getMerchantName() != null ? merchantInfoVO.getMerchantName() : "");
        int creditLevel = merchantInfoVO.getCreditLevel();
        if (creditLevel < 400)
            setMerchantLevel("1.0");// 商户等级
        else if (400 <= creditLevel && creditLevel < 1600)
            setMerchantLevel("2.0");// 商户等级
        else if (1600 <= creditLevel && creditLevel < 3600)
            setMerchantLevel("3.0");// 商户等级
        else if (3600 <= creditLevel && creditLevel < 6400)
            setMerchantLevel("4.0");// 商户等级
        else if (6400 <= creditLevel)
            setMerchantLevel("5.0");// 商户等级
    }

    /**
     * 是否已收藏
     */
    public void isCollect() {
        BusinessCollect.isCollectApi(this, merchantInfoVO.getId(), mHandler);
    }


    /**
     * 设置商户等级
     *
     * @param grade
     */
    private void setMerchantLevel(String grade) {
        switch (grade) {
            case "1.0":
                merchantIvGrade1.setVisibility(View.VISIBLE);
                merchantIvGrade2.setVisibility(View.INVISIBLE);
                merchantIvGrade3.setVisibility(View.INVISIBLE);
                merchantIvGrade4.setVisibility(View.INVISIBLE);
                merchantIvGrade5.setVisibility(View.INVISIBLE);
                break;
            case "2.0":
                merchantIvGrade1.setVisibility(View.VISIBLE);
                merchantIvGrade2.setVisibility(View.VISIBLE);
                merchantIvGrade3.setVisibility(View.INVISIBLE);
                merchantIvGrade4.setVisibility(View.INVISIBLE);
                merchantIvGrade5.setVisibility(View.INVISIBLE);
                break;
            case "3.0":
                merchantIvGrade1.setVisibility(View.VISIBLE);
                merchantIvGrade2.setVisibility(View.VISIBLE);
                merchantIvGrade3.setVisibility(View.VISIBLE);
                merchantIvGrade4.setVisibility(View.INVISIBLE);
                merchantIvGrade5.setVisibility(View.INVISIBLE);
                break;
            case "4.0":
                merchantIvGrade1.setVisibility(View.VISIBLE);
                merchantIvGrade2.setVisibility(View.VISIBLE);
                merchantIvGrade3.setVisibility(View.VISIBLE);
                merchantIvGrade4.setVisibility(View.VISIBLE);
                merchantIvGrade5.setVisibility(View.INVISIBLE);
                break;
            case "5.0":
                merchantIvGrade1.setVisibility(View.VISIBLE);
                merchantIvGrade2.setVisibility(View.VISIBLE);
                merchantIvGrade3.setVisibility(View.VISIBLE);
                merchantIvGrade4.setVisibility(View.VISIBLE);
                merchantIvGrade5.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.merchant_ll_collect_layout)
    public void onClick() {
        if (isMerchantsCollected)
            cancelCollect();
        else
            collectMerchants();
    }

    /**
     * 取消收藏
     */
    private void cancelCollect() {
        if (merchantInfoVO == null)
            return;
        BusinessCollect.cancelStore(this, merchantInfoVO.getId(), mHandler);
    }

    /**
     * 收藏店铺
     */
    private void collectMerchants() {
        //TODO dataMallVO.getMerchant().getHomePageImages() 暂时为空 店铺图片
        collectType = Constant.STORE_MERCHANTS;
        if (merchantInfoVO == null)
            return;
        BusinessCollect.store(this, merchantInfoVO.getMerchantName(), "", Constant.STORE_MERCHANTS, merchantInfoVO.getId(),
                "", merchantInfoVO.getHeadPortrait(), "", StringUtil.jointString(URL.URL_SHOW_MERCHANT_HOME_COLLECT, merchantInfoVO.getId()), mHandler);
    }

    static class ViewPagerAdapter extends FragmentPagerAdapter {

        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }

    }
}
